package com.wdy.service.impl;

import java.util.LinkedList;
import java.util.List;

import com.wdy.dao.BuildingDao;
import com.wdy.po.Building;
import com.wdy.service.BuildingManager;
import com.wdy.util.PageUtil;
import com.wdy.vo.Condition;
import com.wdy.vo.Page;
import com.wdy.vo.Result;
@SuppressWarnings("unchecked")
public class BuildingManagerImpl implements BuildingManager {

	private BuildingDao buildingDao;

	public void setBuildingDao(BuildingDao buildingDao) {
		this.buildingDao = buildingDao;
	}

	public void saveBuilding(Building building) {
		buildingDao.save(building);
	}

	public void removeBuilding(Building building) {
		buildingDao.delete(building);
	}

	public void updateBuilding(Building building) {
		buildingDao.attachDirty(building);
	}

	public Building getBuilding(Integer id) {
		return buildingDao.findById(id);
	}

	public List<Building> listAll() {
		return buildingDao.findAll();
	}

	public List<Building> findByExample(Building building) {
		return buildingDao.findByExample(building);
	}

	public List<Building> findByAddress(String value) {
		return buildingDao.findByAddress(value);
	}

	@Override
	public Result query(List<Condition> conList, Page page) {
		// TODO Auto-generated method stub
		int totalRecords = buildingDao.getQueryCount(conList);
		page = PageUtil.createPage(page, totalRecords);
		List tempList = (List) buildingDao.query(conList, page);
		List<Building> buildings = new LinkedList<Building>();
		for(Object o: tempList){
			if(o instanceof Object[]){
				Object[] oList = (Object[]) o;
				for(Object oo: oList){
					if(oo instanceof Building){
						buildings.add((Building) oo);
					}
				}
			}else{
				if(o instanceof Building){
					buildings.add((Building) o);
				}
			}
		}
		return new Result(page, buildings);
	}

	@Override
	public void removeBuildings(Object[] objects) {
		// TODO Auto-generated method stub
		buildingDao.deleteBatch(objects);
	}

}
